package com.hhf.rpc.core.enums;

import lombok.Getter;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author hhf
 * @Date 2024/10/19
 * @Description
 */
@Getter
public enum ProtocolMessageSerializerEnum {

    /**
     * jdk
     */
    JDK(0, "jdk"),
    /**
     * json
     */
    JSON(1, "json"),
    /**
     * kryo
     */
    KRYO(2, "kryo"),
    /**
     * hessian
     */
    HESSIAN(3, "hessian");


    private final int key;

    private final String value;

    ProtocolMessageSerializerEnum(int key, String value) {
        this.key = key;
        this.value = value;
    }

    /**
     * 获取value值
     * @return
     */
    public static List<String> getValues(){
        return Arrays.stream(ProtocolMessageSerializerEnum.values())
                .map(item -> item.value)
                .collect(Collectors.toList());
    }

    /**
     * 根据key获取枚举类
     * @param key
     * @return
     */
    public static ProtocolMessageSerializerEnum getSerializerByKey(int key){
        for (ProtocolMessageSerializerEnum serializerEnum: ProtocolMessageSerializerEnum.values()){
            if (key == serializerEnum.key){
                return serializerEnum;
            }
        }
        return null;
    }

    /**
     * 根据value获取枚举类
     * @param value
     * @return
     */
    public static ProtocolMessageSerializerEnum getSerializerByValue(String value){
        for (ProtocolMessageSerializerEnum serializerEnum: ProtocolMessageSerializerEnum.values()){
            if (value == serializerEnum.value){
                return serializerEnum;
            }
        }
        return null;
    }

}
